package presentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.RectangleInsets;

import control.CControl;

public class PLiveview extends JPanel implements ComponentListener{
	private static final long serialVersionUID = 1L;
	private ChartPanel speedPanel, rpmPanel, speedGraph, rpmGraph;
	private JFreeChart speedDial, rpmDial;
	private PSmallGraphPanel graphs;
	private CControl cControl;

	public PLiveview(CControl cControl) {
		this.addComponentListener(this);
		this.setLayout(new GridBagLayout());
		this.cControl = cControl;
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		
		this.setBackground(Color.white);

		/** Webcam-image */
		PVideoPanel pVideoPanel = new PVideoPanel(cControl);
		this.cControl.addVideoObserver(pVideoPanel);
		pVideoPanel.setMaximumSize(new Dimension(800,600));
		c.weighty = 0;
		c.weightx = 0;
		c.fill = GridBagConstraints.NONE;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 2;
		this.add(pVideoPanel, c);
        
		/** Speedometer */
		PSpeedDial speed = new PSpeedDial(cControl.getRegisterBoundaries("FSSpeed"));
		speedDial = new JFreeChart(speed);
		speedDial.setBackgroundPaint(Color.white);
		speedPanel = new ChartPanel(speedDial);
		int size = 230;
		speedPanel.setMinimumSize(new Dimension(size,size));
		speedPanel.setPreferredSize(new Dimension(size,size));
		speedPanel.setMaximumSize(new Dimension(size,size));

		
		cControl.addRegisterObserver("FSN_Speed", speed);
		
		c.weightx = 0.5;
		c.weighty = 0;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		//this.add(speedPanel, c);
		
		/** SoC */
		PBatteryJuiceDial soc = new PBatteryJuiceDial();
		JFreeChart socDial = new JFreeChart(soc);
		socDial.setBackgroundPaint(Color.white);
		ChartPanel socPanel = new ChartPanel(socDial);
		socPanel.setMinimumSize(new Dimension(size,size));
		socPanel.setPreferredSize(new Dimension(size,size));
		socPanel.setMaximumSize(new Dimension(size,size));
		cControl.addRegisterObserver("MBN_BMS_SOCHV", soc);
		
		c.weightx = 0;
		c.weighty = 0;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 2;
		
		
		
		JPanel ptem = new JPanel();
		
		BoxLayout b = new BoxLayout(ptem,BoxLayout.Y_AXIS);
		
		
		ptem.setLayout(b);
		ptem.setBackground(Color.white);
		
		JPanel temporary = new JPanel();
		BoxLayout b2 = new BoxLayout(temporary, BoxLayout.X_AXIS);
		temporary.setLayout(b2);
		temporary.add(speedPanel);
		temporary.add(socPanel);
		ptem.add(temporary);
		

		
		/** Temperatures*/
		
		JPanel tp = new JPanel(new GridLayout(2,3,10,10));
		
		tp.setBackground(Color.white);
		
		addThermometer(tp, cControl, "RSN_OilTemp1","Cooleant Motor out", 80.0, 100.0);
		addThermometer(tp, cControl, "RSN_OilTemp2","Cooleant Heat sink out", 80.0, 100.0);
		addThermometer(tp, cControl, "RSN_OilTemp3","Cooleant Heat sink in", 80.0, 100.0);
		addThermometer(tp, cControl, "MBN_MC_HeatsinkTemp","Motor Controller Heatsink", 75.0,80.0);
		addThermometer(tp, cControl, "MBN_MO_StatorTemp","Motor Stator", 85.0,90.0);
		addThermometer(tp, cControl, "MBN_MO_RotorTemp","Motor Rotor", 95.0,100.0);
		
		tp.setPreferredSize(new Dimension(500,400));
		ptem.add(tp);
		
		this.add(ptem,c);
		
//		/*placeholder panel*/
//		c.weightx = 0.5;
//		c.weighty = 0.5;
//		c.fill = GridBagConstraints.NONE;
//		c.gridx = 1;
//		c.gridy = 0;
//		c.gridwidth = 1;
//		c.gridheight = 3;
//		this.add(new JPanel(),c);
		
		/** Graphs panel*/
		speedGraph = new ChartPanel(PChartFactory.createFixedXYGraph("Sample", "Brake [Bar]",cControl.getRegisterBoundaries("FSN_Brake1"),20));
		speedGraph.setPreferredSize(new Dimension(0,120));
		rpmGraph = new ChartPanel(PChartFactory.createFixedXYGraph("Sample", "Throttle [%]",cControl.getRegisterBoundaries("MBN_TorqueSensor1"),10));
		rpmGraph.setPreferredSize(new Dimension(0,120));
		graphs = new PSmallGraphPanel(speedGraph,rpmGraph);
		cControl.addRegisterObserver("FSN_Brake1", graphs);
		cControl.addRegisterObserver("MBN_TorqueSensor2", graphs);
		

		/*placeholder panel*/
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 1;
		this.add(graphs,c);
		
		/** Datalogger status panel*/
		JPanel dl_status = new JPanel();
		dl_status.setBackground(Color.white);
		dl_status.setLayout(new GridLayout(5,1));
		dl_status.add(new PLabel(cControl, "DLN_TimeLeft", "Time left:"));
		dl_status.add(new PLabel(cControl, "DLN_SpaceLeft", "Space left"));
		dl_status.add(new PLabel(cControl, "DLN_LogNR", "Log Number:"));
		dl_status.add(new PLabel(cControl, "DLN_LogTime", "Log Time:"));
		dl_status.add(new PLabel(cControl, "DLN_DatalogCounter", "Datalog Counter"));

		c.weightx = 0.5;
		c.weighty = 0.5;
		c.fill = GridBagConstraints.NONE;
		c.gridx = 3;
		c.gridy = 3;
		c.gridwidth = 1;
		c.gridheight = 1;
		
		this.add(dl_status,c);

		
		/** LV System status panel*/
		JPanel lv_status = new JPanel();
		lv_status.setBackground(Color.white);
		lv_status.setLayout(new GridLayout(5,1));
		lv_status.add(new PLabel(cControl, "LPN_V_LV_Supply", "LV Voltage"));
		lv_status.add(new PLabel(cControl, "LPN_W_LV_Supply", "LV Power"));
		lv_status.add(new PLabel(cControl, "MBN_BMS_Voltage", "HV Voltage"));
		lv_status.add(new PLabel(cControl, "MBN_BMS_Current", "HV Current"));
		lv_status.add(new PLabel(cControl, "MBN_MC_ActualTorque", "Motor Torque"));

		c.weightx = 0.5;
		c.weighty = 0.5;
		c.fill = GridBagConstraints.NONE;
		c.gridx = 3;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 1;
		
		this.add(lv_status,c);
		
		
		/** Safety status*/
		c.weightx = 0.5;
		c.weighty = 0.0;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 3;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 2;
		PSafetyStatus ss = new PSafetyStatus(cControl);
		
		ss.setMinimumSize(new Dimension(120,400));
		ss.setMaximumSize(new Dimension(120,400));
		this.add(ss, c);
		
		
	
		
		/** GPS-map */
		c.weightx = 0;
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 1;
		//this.add(new PSteeringWheel(cControl), c);
		
		/** Dashboard settings */
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 2;
		c.gridwidth = 2;
		c.gridheight = 1;
		//this.add(new PDashboardPanel(cControl), c);
	}
	
	private void addThermometer(JPanel layout,CControl cControl,String name,String visname,Double warning,Double critical)
	{
		PThermometer t = new PThermometer(cControl.getRegisterBoundaries(name), warning,critical);
		
		cControl.addRegisterObserver(name, t);
		JFreeChart tempChart = new JFreeChart(visname,t);
		tempChart.setBackgroundPaint(Color.white);
		ChartPanel tempPanel = new ChartPanel(tempChart);
		tempPanel.setPreferredSize(new Dimension(200,300));
		layout.add(tempPanel);
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {}

	@Override
	public void componentMoved(ComponentEvent arg0) {}

	@Override
	public void componentResized(ComponentEvent arg0) {
		//int frameHeight = this.getHeight();
		int dialWidth = (this.getWidth() - 640)/2;
		int newDialHeight = (dialWidth > 240) ? 240 : dialWidth;
		int graphHeight = 480 - newDialHeight;
		
		//speedPanel.setPreferredSize(new Dimension(0,newDialHeight));
		//speedDial.setPadding(new RectangleInsets(
				//0,(dialWidth-newDialHeight)/2,0,(dialWidth-newDialHeight)/2));
		//rpmPanel.setPreferredSize(new Dimension(0,newDialHeight));
		//rpmDial.setPadding(new RectangleInsets(
				//0,(dialWidth-newDialHeight)/2,0,(dialWidth-newDialHeight)/2));
		
		if (graphHeight > dialWidth) {
			//speedGraph.setPreferredSize(new Dimension(0,(480-newDialHeight)/2));
			//graphs.setLayout(new GridLayout(2,1,0,0));
		} else {
			//speedGraph.setPreferredSize(new Dimension(0,(480-newDialHeight)));
			//graphs.setLayout(new GridLayout(1,2,0,0));
		}
		this.updateUI();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {}
}




